package com.cpi.cornpp.recurrentcharges.action;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.MappingDispatchAction;
import org.apache.struts.util.MessageResources;

import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.costelement.exception.CostElementException;
import com.cpi.cornpp.costelement.services.CostElementServices;
import com.cpi.cornpp.currency.service.CurrencyService;
import com.cpi.cornpp.plants.services.PlantService;
import com.cpi.cornpp.recurrentcharges.exception.RecurrentChargesException;
import com.cpi.cornpp.recurrentcharges.form.RecurrentChargesAddForm;
import com.cpi.cornpp.recurrentcharges.form.RecurrentChargesForm;
import com.cpi.cornpp.recurrentcharges.service.RecurrentChargesService;
import com.cpi.cornpp.recurrentcharges.view.RecurrentChargesGroupView;
import com.cpi.cornpp.recurrentcharges.view.RecurrentChargesView;
import com.cpi.cornpp.transports.exception.TransportException;
import com.cpi.cornpp.transports.service.TransportService;
import com.cs.common.utils.NumberUtil;

public class RecurrentChargesAction extends MappingDispatchAction {
	private final static String DEFAULT_FORWARD = "default";//$NON-NLS-1$
	private static final String PLANT_LIST = "PLANT_LIST";//$NON-NLS-1$
	private final static String COST_ELEMENT_LIST = "COST_ELEMENT_LIST";//$NON-NLS-1$
	private static final String TRANSPORT_LIST = "TRANSPORT_LIST";//$NON-NLS-1$
	private static final String CURRENCY_LIST = "CURRENCY_LIST";//$NON-NLS-1$
	private static final String VERSION_ID = "versionId";//$NON-NLS-1$
	private static final String MESSAGE = "MESSAGE";//$NON-NLS-1$
	private final static String ERROR_MESSAGE = "ERROR_MESSAGE";//$NON-NLS-1$
	private static final String RECURRENT_CHARGES_GROUP_VIEW = "RECURRENT_CHARGES_GROUP_VIEW";//$NON-NLS-1$
	public ActionForward showRecurrentCharges(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws RecurrentChargesException {
		RecurrentChargesService recurrentChargesService = null;
		int versionId = 0;
		HttpSession httpSession = null;
		List recurrentChargesGroupViews = null;
		RecurrentChargesForm recurrentChargesForm = null;
		try{
			recurrentChargesForm = (RecurrentChargesForm)form;
			recurrentChargesService = new RecurrentChargesService();
			httpSession = request.getSession();
			versionId = NumberUtil.getInt(httpSession.getAttribute(VERSION_ID));
			recurrentChargesGroupViews = recurrentChargesService.getRecurrentCharges(versionId);
			fillForm(recurrentChargesForm, recurrentChargesGroupViews);
			request.setAttribute(RECURRENT_CHARGES_GROUP_VIEW, recurrentChargesGroupViews);
		}catch(RecurrentChargesException recurrentChargesException){
			throw recurrentChargesException;
		}catch(Exception exception){
			RecurrentChargesException recurrentChargesException = null;
			exception.printStackTrace(System.out);
			recurrentChargesException= new RecurrentChargesException(exception , CornPPException.LAYER_ACTION,CornPPException.ACTION_LISTS);
			throw recurrentChargesException;
		}
		return mapping.findForward(DEFAULT_FORWARD);
	}
	
	public ActionForward saveRecurrentCharges(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws RecurrentChargesException{
		RecurrentChargesService recurrentChargesService = null;
		RecurrentChargesForm recurrentChargesForm = null;
		HttpSession httpSession = null;
		MessageResources messageResources = null;
		try{
			messageResources = getResources(request);
			recurrentChargesForm = (RecurrentChargesForm)actionForm;
			recurrentChargesService = new RecurrentChargesService();
			httpSession = request.getSession();
			recurrentChargesService.updateRecurrentCharges(recurrentChargesForm.getRecurrentChargesAmountViews());
			httpSession.setAttribute(MESSAGE , messageResources.getMessage("label.message.recurrentcharge.update.success"));
		}catch(RecurrentChargesException recurrentChargesException){
			throw recurrentChargesException;
		}catch(Exception exception){
			RecurrentChargesException recurrentChargesException = null;
			exception.printStackTrace(System.out);
			recurrentChargesException= new RecurrentChargesException(exception , CornPPException.LAYER_ACTION,CornPPException.ACTION_UPDATE);
			throw recurrentChargesException;
		}
		return mapping.findForward(DEFAULT_FORWARD);
	}
	
	public ActionForward deleteRecurrentCharges(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws RecurrentChargesException{
		RecurrentChargesService recurrentChargesService = null;
		RecurrentChargesAddForm recurrentChargesAddForm = null;
		int versionId = 0;
		HttpSession httpSession = null;
		MessageResources messageResources = null;
		try{
			messageResources = getResources(request);
			recurrentChargesAddForm = (RecurrentChargesAddForm)actionForm;
			recurrentChargesService = new RecurrentChargesService();
			httpSession = request.getSession();
			versionId = NumberUtil.getInt(httpSession.getAttribute(VERSION_ID));
			recurrentChargesAddForm.getRecurrentChargesAddView().setVersionId(versionId);
			recurrentChargesService.deleteRecurrentCharges(recurrentChargesAddForm.getRecurrentChargesAddView());
			httpSession.setAttribute(MESSAGE , messageResources.getMessage("label.message.recurrentcharge.delete.success"));
		}catch(RecurrentChargesException recurrentChargesException){
			throw recurrentChargesException;
		}catch(Exception exception){
			RecurrentChargesException recurrentChargesException = null;
			exception.printStackTrace(System.out);
			recurrentChargesException= new RecurrentChargesException(exception , CornPPException.LAYER_ACTION,CornPPException.ACTION_UPDATE);
			throw recurrentChargesException;
		}
		return mapping.findForward(DEFAULT_FORWARD);
	}
	
	public ActionForward addRecurrentCharges(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws RecurrentChargesException, CostElementException, TransportException {
		PlantService plantService = null;
		CostElementServices costElementServices = null;
		TransportService transportService = null;
		CurrencyService currencyService = null;
		List plantViews = null;
		Collection costElementViews = null;
		List transportViews = null;
		List currencyViews = null;
		try{
			plantService = new PlantService();
			costElementServices = new CostElementServices();
			transportService = new TransportService();
			currencyService = new CurrencyService();
			costElementViews = costElementServices.getCostElementList();
			transportViews = transportService.getTransports();
			plantViews = plantService.list();
			currencyViews = currencyService.getCurrencies();
			request.setAttribute(PLANT_LIST, plantViews);
			request.setAttribute(COST_ELEMENT_LIST, costElementViews);
			request.setAttribute(TRANSPORT_LIST, transportViews);
			request.setAttribute(CURRENCY_LIST, currencyViews);
		}catch(TransportException transportException){
			throw transportException;
		}catch(CostElementException costElementException){
			throw costElementException;
		}catch(Exception exception){
			RecurrentChargesException recurrentChargesException = null;
			exception.printStackTrace(System.out);
			recurrentChargesException= new RecurrentChargesException(exception , CornPPException.LAYER_SERVICE,CornPPException.ACTION_LISTS);
			throw recurrentChargesException;
		}
		return mapping.findForward(DEFAULT_FORWARD);
	}
	
	/**
	 * 
	 * @param mapping
	 * @param actionForm
	 * @param request
	 * @param response
	 * @return
	 * @throws RecurrentChargesException
	 */
	public ActionForward saveAddRecurrentCharges(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws RecurrentChargesException{
		RecurrentChargesService service = null;
		RecurrentChargesAddForm form = null;
		HttpSession httpSession = null;
		MessageResources messageResources = null;
		int versionId = 0;
		try{
			messageResources = getResources(request);
			httpSession = request.getSession();
			versionId = NumberUtil.getInt(httpSession.getAttribute(VERSION_ID));
			service = new RecurrentChargesService();
			form = (RecurrentChargesAddForm)actionForm;
			form.getRecurrentChargesAddView().setVersionId(versionId);
			if(!service.existRecurrentCharges(form.getRecurrentChargesAddView())){
				service.insertRecurrentCharges(form.getRecurrentChargesAddView());
				httpSession.setAttribute(MESSAGE , messageResources.getMessage("label.message.recurrentcharge.insert.success"));
			}else{
				httpSession.setAttribute(ERROR_MESSAGE , messageResources.getMessage("label.error.message.recurrentcharge.exists"));
			}
			form.reset(mapping, request);
		}catch(RecurrentChargesException recurrentChargesException){
			throw recurrentChargesException;
		}catch(Exception exception){
			RecurrentChargesException recurrentChargesException = null;
			exception.printStackTrace(System.out);
			recurrentChargesException= new RecurrentChargesException(exception , CornPPException.LAYER_SERVICE,CornPPException.ACTION_LISTS);
			throw recurrentChargesException;
		}
		return mapping.findForward(DEFAULT_FORWARD);
	}
	
	private void fillForm(RecurrentChargesForm recurrentChargesForm,List recurrentChargesGroupViews)throws RecurrentChargesException{
		List recurrentChargesAmountViews = null;
		Iterator groupIterator = null;
		Iterator chargesIterator = null;
		RecurrentChargesGroupView recurrentChargesGroupView = null;
		RecurrentChargesView recurrentChargesView = null;
		try{
			recurrentChargesAmountViews = new ArrayList(0);
			groupIterator = recurrentChargesGroupViews.iterator();
			while(groupIterator.hasNext()){
				recurrentChargesGroupView = (RecurrentChargesGroupView)groupIterator.next();
				chargesIterator = recurrentChargesGroupView.getRecurrentChargesViews().iterator();
				while( chargesIterator.hasNext() ){
					recurrentChargesView = (RecurrentChargesView)chargesIterator.next();
					recurrentChargesAmountViews.addAll(recurrentChargesView.getRecurrentChargesAmountViews());
				}
			}
			recurrentChargesForm.setRecurrentChargesAmountViews(recurrentChargesAmountViews);
		}catch(Exception exception){
			RecurrentChargesException recurrentChargesException = null;
			exception.printStackTrace(System.out);
			recurrentChargesException= new RecurrentChargesException(exception , CornPPException.LAYER_ACTION,CornPPException.ACTION_LISTS);
			throw recurrentChargesException;
		}
	}
}